package ltd.newbee.mall.api.mall;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import ltd.newbee.mall.api.mall.vo.NewBeeMallGoodsDetailVO;
import ltd.newbee.mall.api.mall.vo.NewBeeMallSearchGoodsVO;
import ltd.newbee.mall.common.Constants;
import ltd.newbee.mall.common.NewBeeMallException;
import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.config.annotation.TokenToMallUser;
import ltd.newbee.mall.entity.MallUser;
import ltd.newbee.mall.entity.NewBeeMallGoods;
import ltd.newbee.mall.entity.PageQuery;
import ltd.newbee.mall.entity.PageResult;
import ltd.newbee.mall.service.NewBeeMallGoodsService;
import ltd.newbee.mall.util.BeanUtil;
import ltd.newbee.mall.util.Result;
import ltd.newbee.mall.util.ResultGenerator;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Tag(name = "4.新蜂商城商品相关接口")
@RequestMapping("/api/v1")
public class NewBeeMallGoodsAPI {
    @Resource
    private NewBeeMallGoodsService newBeeMallGoodsService;

    @GetMapping("/search")
    @Operation(summary = "商品搜索接口", description = "根据关键字和分类id进行搜索")
    public Result<PageResult<NewBeeMallSearchGoodsVO>> search(
            @RequestParam(required = false) @Parameter(name = "keyword", description = "搜索关键字") String keyword,
            @RequestParam(required = false) @Parameter(name = "goodsCategoryId", description = "分类id") Long goodsCategoryId,
            @RequestParam(required = false) @Parameter(name = "orderBy", description = "排序") String orderBy,
            @RequestParam(required = false) @Parameter(name = "pageIndex", description = "页码") Integer pageIndex,
            @RequestParam(required = false, defaultValue = "20") @Parameter(name = "pageSize", description = "大小") Integer pageSize,
            @Parameter(hidden = true) @TokenToMallUser MallUser loginMallUser
    ) {
        Map params = new HashMap(8);
        //两个搜索参数都为空，直接返回异常
        if (goodsCategoryId == null && !StringUtils.hasText(keyword)) {
            NewBeeMallException.fail("非法的搜索参数");
        }
        if (pageIndex == null || pageIndex < 1) {
            pageIndex = 1;
        }
        params.put("goodsCategoryId", goodsCategoryId);
        params.put("pageIndex", pageIndex);
        params.put("pageSize", pageSize);
        //对keyword做过滤 去掉空格
        if (StringUtils.hasText(keyword)) {
            params.put("keyword", keyword);
        }
        if (StringUtils.hasText(orderBy)) {
            params.put("orderBy", orderBy);
        }
        params.put("goodsSellStatus", Constants.SELL_STATUS_UP);
        PageQuery pageQuery = new PageQuery(params);
        return ResultGenerator.genSuccessResult(newBeeMallGoodsService.searchNewBeeMallGoods(pageQuery));
    }

    @GetMapping("/goods/detail/{goodsId}")
    @Operation(summary = "商品详情接口", description = "根据商品id获取商品详情")
    public Result<NewBeeMallGoodsDetailVO> goodsDetail(
            @Parameter(name = "goodsId", description = "商品id") @PathVariable("goodsId") Long goodsId,
            @Parameter(hidden = true) @TokenToMallUser MallUser loginMallUser
    ) {
        if (goodsId < 1) {
            return ResultGenerator.genFailResult("参数异常");
        }
        NewBeeMallGoods newBeeMallGoods = newBeeMallGoodsService.getNewBeeMallGoodsById(goodsId);
        if (Constants.SELL_STATUS_UP != newBeeMallGoods.getGoodsSellStatus()) {
            NewBeeMallException.fail(ServiceResultEnum.GOODS_PUT_DOWN.getResult());
        }

        NewBeeMallGoodsDetailVO newBeeMallGoodsDetailVO = new NewBeeMallGoodsDetailVO();
        BeanUtil.copyProperties(newBeeMallGoods, newBeeMallGoodsDetailVO);
        newBeeMallGoodsDetailVO.setGoodsCarouselList(newBeeMallGoods.getGoodsCarousel().split(","));
        return ResultGenerator.genSuccessResult(newBeeMallGoodsDetailVO);
    }
}
